package com.yiren.dbaa.module.help_center.view.fragment;

import android.graphics.Bitmap;
import android.webkit.JavascriptInterface;

import com.google.gson.Gson;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.dbaa.R;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.fragment.LazyLoadFragment;
import com.yiren.dbaa.view.X5WebView;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.HashMap;

import butterknife.BindView;

/**
 * @author lwb
 * @date :2022/7/28 10:12
 * @description: 联系我们
 */
public class WebViewFragment extends LazyLoadFragment {

    @BindView(R.id.xwv_public)
    X5WebView xwvPublic;

    @Override
    protected int setContentView() {
        return R.layout.fragment_webview;
    }

    @Override
    protected void init() {
        loadUrl(AddressConstant.getGzGzhNewYdyUrl());
    }

    private void loadUrl(String url){
        LoadDialog.show(getActivity(),"正在加载中...");
        xwvPublic.loadUrl(url);
        xwvPublic.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                // 网页加载完成时处理 如：让 加载对话框 消失
                LoadDialog.dismiss(getActivity());
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("token", LoadConfigApi.loadUserParamConfig(getActivity()).getAccessToken());
                map.put("equipmentType", "Android");
                view.loadUrl("javascript:initWebView(" + new Gson().toJson(map) + ")");
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                // 开始加载网页时处理 如：显示"加载提示" 的加载对话框
                LoadDialog.show(getActivity(), "加载中...");
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                // 加载网页失败时处理 如：提示失败，或显示新的界面
                MessageBox.show(getActivity(), "网页加载出错了！");
            }
        });

//        xwvPublic.addJavascriptInterface(new JSInterface(), "native");
//        WebSettings settings = xwvPublic.getSettings();
//        settings.setLoadWithOverviewMode(true);
//        settings.setUseWideViewPort(true);
//        settings.setJavaScriptEnabled(true);
//        settings.setDefaultTextEncodingName("utf-8");
//        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//        xwvPublic.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // 设置缓存模式
//        // 开启 DOM storage API 功能
//        xwvPublic.getSettings().setDomStorageEnabled(true);
//        // 开启 database storage API 功能
//        xwvPublic.getSettings().setDatabaseEnabled(true);
//        String cacheDirPath = getActivity().getFilesDir().getAbsolutePath() + "";
//        // String cacheDirPath = getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;
//        // 设置数据库缓存路径
//        xwvPublic.getSettings().setDatabasePath(cacheDirPath);
//        // 设置 Application Caches 缓存目录
//        xwvPublic.getSettings().setAppCachePath(cacheDirPath);
//        // 开启 Application Caches 功能
//        xwvPublic.getSettings().setAppCacheEnabled(true);

    }


    class JSInterface {
        @JavascriptInterface
        public void closeWebview() {
            getActivity().finish();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
